/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphic.entites;

import graphic.Movable;
import javafx.geometry.Dimension2D;
import javafx.geometry.Point2D;
import javafx.scene.paint.*;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;

/**
 *
 * @author David
 */
public class Class extends Movable
{
  public static final Color BORDER_COLOR = ColorBuilder.create().red(0.2).blue(0.2).green(0.2).build();
  public static final Color FILL_COLOR = ColorBuilder.create().red(1.0).green(0.9).blue(0.55).build();
  public static final double STROKE_WIDTH = 1.3;
  public static final Dimension2D DEFAULT_SIZE = new Dimension2D(150.0, 80.0);
  
  private UML.Class modelClass;
  
  public Class(UML.Class modelClass)
  {
    initialize(modelClass);
  }
  
  public Class(UML.Class modelClass, Point2D location)
  {
    super(location);
    initialize(modelClass);
  }
  
  private void initialize(UML.Class modelClass){
    this.modelClass = modelClass;
    
    Rectangle border = createBorder(new Dimension2D(getBoundsInParent().getWidth(), getBoundsInParent().getWidth()));
    Text textClassName = TextBuilder.create().text(modelClass.getName()).build();
    textClassName.setTranslateX((getBoundsInParent().getWidth() - textClassName.getBoundsInParent().getWidth()) / 2.0);
    textClassName.setTranslateY(10.0);
    
    System.out.println(getBoundsInLocal().getWidth());
    
    getChildren().addAll(border, textClassName);
    
    setStyle("-fx-background-color: yellow; -fx-border-style: solid; -fx-border-width: 1px; -fx-border-color: black;");
  }
  
  public UML.Class getModel(){
    return modelClass;
  }
  
  public static Rectangle createBorder(Dimension2D size)
  {
    LinearGradient lg =
    LinearGradientBuilder.create().cycleMethod(CycleMethod.NO_CYCLE)
                                  .startX(0.0)
                                  .startY(0.0)
                                  .endX(1.0)
                                  .endY(0.0)
                                  .stops(StopBuilder.create().color(FILL_COLOR).offset(0.0).build(),
                                         StopBuilder.create().color(FILL_COLOR.darker()).offset(1.0).build())
                                  .build();
    
    Rectangle r = new Rectangle(size.getWidth(), size.getHeight());
    r.setFill(lg);
    r.setStroke(BORDER_COLOR);
    r.setStrokeWidth(STROKE_WIDTH);
    
    return r;
  }
}
